//
//  SourceViewController.m
//  NtoccShipper
//
//  Created by mac on 2019/11/13.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "SourceViewController.h"
#import "SourceListVC.h"
#import "SourcesOrderVC.h"
#import "StrongReminderView.h"
#import "SourcesListSearchVC.h"
#import <WZLBadge/WZLBadgeImport.h>

@interface SourceViewController ()
@property (nonatomic,strong) CJButton *btnSearch;
@property (nonatomic,strong) CJButton *btnOrderList;
@property (copy, nonatomic) NSDictionary *dicSearch;

@end

@implementation SourceViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    //    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

    if ([CJControl isLogin]) {
        if ([CJControl getIdCardDateHasDue] && [CJControl getStrongReminderShow]) {
            [[[StrongReminderView shared]initWithView] show];
        }else{
            [[[StrongReminderView shared]initWithView] hidden];
        }
    }
}


- (void)viewDidLoad {
    // Do any additional setup after loading the view.
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont cjTitleFont18];
    titleLabel.textColor = [UIColor cjMainTextColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"我的货源";
    self.navigationItem.titleView = titleLabel;

    self.dicSearch = [[NSDictionary alloc]init];
    self.titleArray = @[@"全部",@"未接单",@"已接单",@"已装货",@"已卸货",@"已结算",@"已完成",@"已撤销",@"常发货源"];
    self.titles = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];

    self.navigationController.navigationBar.barTintColor = [UIColor cjWhiteColor];

    self.menuViewStyle = WMMenuViewStyleLine;
    self.titleSizeSelected = 16;
    self.titleSizeNormal = 14;
    //
    self.progressViewIsNaughty = YES;
    self.progressWidth = 30;
    self.progressHeight = 2;
    self.titleColorNormal = [UIColor cjMainTextColor];
    self.titleColorSelected = [UIColor cjMainColor];
    //   self.preloadPolicy = WMPageControllerPreloadPolicyNever;
    //         self.menuBGColor = [UIColor whiteColor];
    self.showOnNavigationBar = NO;

    [super viewDidLoad];

    //两个按钮的父类view
    UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, kNavBarHeight)];
    [rightButtonView addSubview:self.btnSearch];
    [rightButtonView addSubview:self.btnOrderList];
    //把右侧的两个按钮添加到rightBarButtonItem
    UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
    self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
    
    CJRegisterNotify(@"clearSourcesBageNum", @selector(clearSourcesBageNum))
}

- (void)clearSourcesBageNum{
    [self.btnSearch clearBadge];
    self.dicSearch = @{
        @"mainOrderNumber":@"",
        @"placeOfReceipt":@"",
        @"coalTypeName":@"",
        @"placeOfShipment":@"",
        @"startGmtCreated":@"",
        @"endGmtCreated":@"",
    };
    [self reloadData];
}

- (void)jumpToOrderList{
    SourcesOrderVC *vc = [[SourcesOrderVC alloc]init];
    vc.strCode = @"";
    [self pushVc:vc];
}


/// 货源单搜索
- (void)searchSourceListAction{

//    NSDictionary *dicPra = @{
//        @"mainOrderNumber":self.tfMainOrderNumber.text,
//        @"placeOfShipment":self.tfPlaceOfShipment.text,
//        @"placeOfReceipt":self.tfPlaceOfReceipt.text,
//        @"coalTypeName":self.tfCoalTypeName.text,
//        @"bageNums":@(bageNums)
//    };
    
    @weakify(self);
    SourcesListSearchVC *vc = [[SourcesListSearchVC alloc]init];
    vc.dictInfo = self.dicSearch;
    vc.blockSerach = ^(id anyObject) {
        
        @strongify(self);
        NSString *strBageNums = CJStringWithFormat(@"%@",anyObject[@"bageNums"]);
        if (strBageNums.intValue <= 0) {
            [self.btnSearch clearBadge];
        }else{
            [self.btnSearch showBadgeWithStyle:WBadgeStyleNumber value:strBageNums.intValue animationType:WBadgeAnimTypeNone];
            self.btnSearch.badgeFrame = CGRectMake(40 - 13, 5, 13, 13);
        }
        
        self.dicSearch = (NSDictionary *)anyObject;
        CJSendNotify(@"searchSourcesListAction", (NSDictionary *)anyObject);
        [self reloadData];
    };
    [self pushVc:vc];
    
}

- (void)pushVc:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}
//1、返回分页数量，可以再这里设置标题栏各种样式
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return 9;
}

//3、把标题与分页对应起来
- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return _titleArray[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    SourceListVC* vc  = [[SourceListVC alloc]init];
    vc.indexPage = self.titles[index];
    vc.dicSearch = self.dicSearch;
//    vc.search_condition = self.titles[index];
//    vc.strJpush = self.strJpush;
//    vc.bidDriverListModel = self.bidDriverListModel;
    return vc;
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
    return width ;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    
    menuView.backgroundColor = [UIColor clearColor];
    //    CGFloat leftMargin = self.showOnNavigationBar ? 0 : 0;
//    CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);
    return CGRectMake(0, kTopHeight, self.view.frame.size.width, 44);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
    
}

- (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = font;
    [label sizeToFit];
    return label.frame.size.width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----------lazy

- (CJButton *)btnSearch{
    if (!_btnSearch) {
        _btnSearch = CJButton.btnInit
        .btnFrame(CGRectMake(20, 0, 40, kNavBarHeight))
//        .btnText(@"运单")
//        .btnNormalTitleColor([UIColor cjMainTextColor])
//        .btnSelectTitleColor([UIColor cjMainTextColor])
        .btnTargetAction(self ,@selector(searchSourceListAction))
        .btnSelectImgName(@"zcj_icon_search_gray")
        .btnNormalImgName(@"zcj_icon_search_gray")
        ;
    }
    return _btnSearch;;
}

- (CJButton *)btnOrderList{
    if (!_btnOrderList) {
        _btnOrderList = CJButton.btnInit
        .btnFrame(CGRectMake(_btnSearch.right, _btnSearch.top, _btnSearch.width, _btnSearch.height))
        .btnText(@"运单")
        .btnNormalTitleColor([UIColor cjMainTextColor])
        .btnSelectTitleColor([UIColor cjMainTextColor])
        .btnTargetAction(self ,@selector(jumpToOrderList))
        ;
    }
    return _btnOrderList;
}


@end
